﻿<navigation:Page x:Class="MoneyBook.Views.MainView" 
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
           xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
           xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
           mc:Ignorable="d"
           xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
           xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
           xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
           xmlns:li="clr-namespace:MoneyBook.Interactivity"
           xmlns:lv="clr-namespace:MoneyBook.Views"
           xmlns:ld="clr-namespace:MoneyBook.Windows.Data"
           xmlns:data="clr-namespace:System.Windows.Data;assembly=System.Windows"
           xmlns:winComp="clr-namespace:System.ComponentModel;assembly=System.Windows"
           xmlns:lc="clr-namespace:MoneyBook.Controls"
           xmlns:lx="clr-namespace:MoneyBook.Xaml"
           xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" 
           d:DesignWidth="640" d:DesignHeight="480"
           x:Name="Self"
           Title="MainView Page">
    <navigation:Page.DataContext>
        <lv:MainViewModel />
    </navigation:Page.DataContext>
    <navigation:Page.Resources>
        <data:CollectionViewSource x:Key="AccountsViewSource">
            <data:CollectionViewSource.SortDescriptions>
                <winComp:SortDescription PropertyName="IsMain" Direction="Descending" />
            </data:CollectionViewSource.SortDescriptions>
        </data:CollectionViewSource>
        <data:CollectionViewSource x:Key="ItemsViewSource">
            <data:CollectionViewSource.GroupDescriptions>
                <data:PropertyGroupDescription Converter="{StaticResource CutoffTimeConverter}" 
                                               PropertyName="PurchaseDate" />
            </data:CollectionViewSource.GroupDescriptions>
        </data:CollectionViewSource>
    </navigation:Page.Resources>
    <Grid x:Name="LayoutRoot">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Loaded">
                <li:InvokeMethodAction MethodName="LoadAccounts" 
                TargetObject="{Binding Path=DataContext,ElementName=Self}" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
        <ld:ViewData.CollectionViewSourceBinders>
            <ld:CollectionViewSourceBinder CollectionViewSource="{StaticResource AccountsViewSource}"
                                              Source="{Binding Path=DataContext.Accounts,ElementName=Self}" />
            <ld:CollectionViewSourceBinder CollectionViewSource="{StaticResource ItemsViewSource}"
                                              Source="{Binding Path=DataContext.Items,ElementName=Self}" />
        </ld:ViewData.CollectionViewSourceBinders>
        <Grid.RowDefinitions>
            <RowDefinition Height="27" />
            <RowDefinition Height="27" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
            <ComboBox Width="100" ItemsSource="{Binding Source={StaticResource AccountsViewSource}}" 
                DisplayMemberPath="AccountName" x:Name="AccountsComboBox">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="SelectionChanged">
                        <li:InvokeMethodAction TargetObject="{Binding DataContext, ElementName=Self}"
                            MethodName="LoadAccountItems">
                            <li:Parameter Value="{Binding SelectedValue, ElementName=AccountsComboBox}" />    
                        </li:InvokeMethodAction>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </ComboBox>
            <HyperlinkButton Content="{Binding ShowCreateAccountButton,Source={StaticResource Strings}}">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="Click">
                        <li:ShowDialogAction
                                Parameter="{Binding DataContext.Accounts, ElementName=Self}"
                                DialogType="{Binding Dialogs.DialogViews.CreateNewAccountDialog, Source={StaticResource DialogWrapper}}">
                            <i:Interaction.Triggers>
                                <li:ActionCompletedTrigger>
                                    <li:InvokeMethodAction TargetObject="{Binding DataContext, ElementName=Self}" 
                                        MethodName="AddedNewAccount">
                                        <li:InvokeParameter PropertyName="Result" />
                                    </li:InvokeMethodAction>
                                </li:ActionCompletedTrigger>
                            </i:Interaction.Triggers>
                        </li:ShowDialogAction>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </HyperlinkButton>
        </StackPanel>
        <StackPanel Grid.Row="1" Orientation="Horizontal">
            <Button Content="{Binding ShowCreateItemButton,Source={StaticResource Strings}}"
                    IsEnabled="{Binding SelectedValue, ElementName=AccountsComboBox, Converter={StaticResource NullToBooleanConverter}}"
                    x:Name="ShowCreateItemButton">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="Click">
                        <li:ShowDialogAction
                                Parameter="{Binding SelectedValue, ElementName=AccountsComboBox}"
                                DialogType="{Binding Dialogs.DialogViews.CreateNewAccountItemDialog, Source={StaticResource DialogWrapper}}">

                            <i:Interaction.Triggers>
                                <li:ActionCompletedTrigger>
                                    <li:InvokeMethodAction TargetObject="{Binding DataContext, ElementName=Self}" 
                                        MethodName="AddedNewItem">
                                        <li:InvokeParameter PropertyName="Result" />
                                    </li:InvokeMethodAction>
                                </li:ActionCompletedTrigger>
                            </i:Interaction.Triggers>

                        </li:ShowDialogAction>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </Button>
        </StackPanel>
        <lc:GroupingView Grid.Row="2" ItemsSource="{Binding Source={StaticResource ItemsViewSource}}">
            <lc:GroupingView.Resources>
                <DataTemplate DataType="{lx:Type Target=System.DateTime}" x:Key="DateTimeTypeDataTemplate">
                    <TextBlock Text="{Binding StringFormat=yyyy/MM/dd}" />
                </DataTemplate>
                <DataTemplate DataType="{lx:Type Target=sys:String}" x:Key="StringTypeDataTemplate">
                    <TextBlock Text="{Binding }" />
                </DataTemplate>
            </lc:GroupingView.Resources>
            <lc:GroupingView.GroupStyle>
                <Style TargetType="lc:GroupingViewGroup">
                    <Setter Property="FontWeight" Value="Bold" />
                </Style>
            </lc:GroupingView.GroupStyle>
            <lc:GroupingView.ItemStyle>
                <Style TargetType="lc:GroupingViewItem">
                    <Setter Property="FontWeight" Value="Normal" />
                </Style>
            </lc:GroupingView.ItemStyle>
            <lc:GroupingView.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Width="100" Text="{Binding Category.CategoryName}" />
                        <TextBlock Text="{Binding Expense, StringFormat='c'}" />
                    </StackPanel>
                </DataTemplate>
            </lc:GroupingView.ItemTemplate>
        </lc:GroupingView>
    </Grid>
</navigation:Page>
